package bud.cpn;

import java.io.PrintStream;
import java.io.PrintWriter;

public class CPNException extends Exception
{
    private static final long serialVersionUID = 1L;
    private Throwable cause;
    private boolean causeRead;

    public CPNException()
    {
        super();
        this.cause = null;
    }

    public CPNException(String message)
    {
        super(message);
        this.cause = null;
    }

    public CPNException(String message, Throwable cause)
    {
        super(message);
        this.cause = cause;
    }

    public CPNException(Throwable cause)
    {
        super(cause.toString());
        this.cause = cause;
    }

    public Throwable getCause()
    {
        this.causeRead = true;
        return this.cause;
    }

    public void printStackTrace()
    {
        printStackTrace(System.err);
    }

    public void printStackTrace(PrintStream ps)
    {
        this.causeRead = false;
        super.printStackTrace(ps);
        if (cause != null && this.causeRead == false)
        {
            ps.print("Caused by: ");
            cause.printStackTrace(ps);
        }
    }

    public void printStackTrace(PrintWriter pw)
    {
        this.causeRead = false;
        super.printStackTrace(pw);
        if (cause != null && this.causeRead == false)
        {
            pw.print("Caused by: ");
            cause.printStackTrace(pw);
        }
    }
}
